package database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SupplierTable extends Table {

	private static final String SUPPLIER_INSERT = "INSERT INTO Supplier VALUES ('%s', '%s', '%s', '%s')";
	private static final String SUPPLIER_DELETE = "DELETE FROM Supplier WHERE supname='%s'";
	private static final String SUPPLIER_DISPLAY = "SELECT * FROM Supplier";	
	
	private static final String SUPPLIER_EXISTS_QUERY = "SELECT * FROM Supplier WHERE supname='%s'";
	
	public enum Column
	{
		SUP_NAME(1, true),
		ADDRESS(2, false),
		CITY(3, false),
		STATUS(4, false);
		
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = {
				"supName",
				"address",
				"city",
				"status"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return index;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}

		public boolean isPrimaryKey() {
			return this.isPrimaryKey;
		}
	};
	
	public SupplierTable()
	{
		super(SUPPLIER_INSERT, SUPPLIER_DELETE, SUPPLIER_DISPLAY, SUPPLIER_EXISTS_QUERY);
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}				
		
		return primaryKeyValues;
	}

	public void addSupplier(String name, String address, String city, String status) throws SQLException
	{
		List<String> supplierTuple = new ArrayList<String>(Column.values().length);
		supplierTuple.add(Column.SUP_NAME.ordinal(), name);
		supplierTuple.add(Column.ADDRESS.ordinal(), address);
		supplierTuple.add(Column.CITY.ordinal(), city);
		supplierTuple.add(Column.STATUS.ordinal(), status);			
			
		insert(supplierTuple);		
	}

	public void removeSupplier(String name) throws SQLException
	{
		List<String> supplierPrimaryKey = new ArrayList<String>();
		supplierPrimaryKey.add(name);
		
		delete(supplierPrimaryKey);		
	}
	
	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
	
}
